#include "kit/event/PrivateMsgEvent.hpp"
#include <string>
#include <vector>
#include "kit/tool/Tokenizer.hpp"

namespace KitEvent
{

	PrivateMsgEvent::~PrivateMsgEvent()
	{

	}

	void PrivateMsgEvent::execute(KitUser::UserMap * iUsers)
	{
		// check if the target exist
		if(this->isValid(iUsers))
		{
			// send the message to the target
			KitUser::User * aTo = iUsers->get(_message->getTo());
			try
			{
				aTo->getConnection()->write(_message->getPayload());
			}
			catch(SystemSocket::WriteException & aException)
			{
				// if we couldn't write anymore on the connection mark it as invalid
				aTo->getConnection()->markInvalid();
			}
		}
	}

	bool PrivateMsgEvent::isValid(KitUser::UserMap * iUsers)
	{
		// it's valid when contains the target user
		return iUsers->contains(_message->getTo());
	}
}
